<?php
/** Copyright 2014 TinPhp Systems, Inc.
 * @author zhangxilian 2014-6-15
 * @package TinPhp
 */
if (!defined('IN_TINPHP')) {
    exit('Access Denied');
}

/** 分页处理类 */
class page {
	/**
	 * @DEMO $par = array ( "keywords" => $_REQUEST["keywords"] );
	 * $pageList = page :: listPage($par, 总记录数, 当前页数, 每页显示, 页数参数名称);
	 * @param array $par 附加其他参数
	 * @param int $total  总记录数
	 * @param int $page 当前页数
	 * @param int $size 每页显示
	 * @param string $p 页数参数名称
	 * @param string $type 显示样式是否最小(mini)
	 * @param int $middlePageNum 中间数字分页前后数，如果等于0 不需要中间数字部分
	 * @param int $goto 是否需要输入式的跳转，
	 * @return string
	 */
    static function listPage($par, $total, $page, $size, $p = "p", $type = null, $middlePageNum=4, $goto=false) 
    {
        $par_string = "";
        foreach ($par as $key => $val ) {
            if (trim ( $val ) == "") continue;
            $par_string .= "&" . trim ( $key ) . "=" . trim ( $val );
        }
        $link = $_SERVER ['PHP_SELF'] . "?".TIN_CTL."=" . $_REQUEST ['ctl'];
        if($_REQUEST ['act']) $link .= "&".TIN_ACT."=" . $_REQUEST ['act'];
        $link .= $par_string;
        
    	/* 第一页 */
        $first = ($page == 1) ? "<a href='javascript:void(0)'>第一页</a>" : "<a href='" . $link . "&" .$p . "=1'>第一页</a>";
    	/* 最后一页 */
        if (ceil ( $total / $size ) == 1) {
            $last = "";
        } else {
            $last = ceil ( $total / $size ) <= $page ? "<a href='javascript:void(0)'>未页</a>" : "<a href='" . $link . "&" .$p . "=" . ceil ( $total / $size ) . "'>末页</a>";
        }

        $pre = $page > 1 ? "<a href='" . $link . "&" .$p . "=" . ($page - 1) . "'>上一页</a></span>" : "<a href='javascript:void(0)'>上一页</a>";
        $next = $page < ceil ( $total / $size ) ? "<a href='" . $link . "&" .$p . "=" . ($page + 1) . "'>下一页</a>" : "<a href='javascript:void(0)'>下一页</a>";

        $z_totalPage = ceil ( $total / $size );
        if ($type == "mini") {
            return "<span id='page_total'>总数<strong>" . $total . "</strong>条&nbsp;&nbsp;页次 <strong>".$page."</strong>/<strong>". $z_totalPage  . "</strong>&nbsp;" . $pre . "&nbsp;" . $next . "";
        }
    	/* 中间部份 */
        $middle = "";
        if($middlePageNum!=0){
	        $front = $page - $middlePageNum;
	        $after = $page + $middlePageNum;
	        //$gotoPageHTML = '<select name="select" onchange="window.location.href=\''.$link.'&'.$p.'=\'+this.options[this.selectedIndex].value">';
	        $middle = '<span class="mid_num">';
	        for($i = $front; $i <= $after; $i++) {
	            if ($i < 1 || $i >= ceil ( $total / $size )+1) { continue; }
	            if($page==$i){
		            $middle .= '<a href="javascript:void(0)" class="cur">'.$i.'</a>';
	            }else{
	            	$middle .= "<a href='" . $link . "&" .$p . "=" . $i . "'>" . $i . "</a>";
	            }
	            //$z_p_sel = '';
	            //if($page==$i) $z_p_sel=' selected="selected"';
	            //$gotoPageHTML .= '<option value="'.$i.'"'.$z_p_sel.'>'.$i.'</option>';
	        }
	        $middle .= '</span>';
	        //$gotoPageHTML .= '</select>';
        }
		if($goto){
	        $gotoHtm = "转到<input type='text' id='goto_page' value='" . $page . "' onfocus='this.select();'" . " size='".$p."' />页";
	        $gotoHtm .= "<input type='button' value='GO'" . " onclick=\"window.location.href='" . $link . "&" .$p . "='+document.getElementById('goto_page').value+''\" />";
		}
        return "<span>总数<strong>" . $total . "</strong>条&nbsp;页次<strong> ".$page."</strong>/<strong>". $z_totalPage ."</strong>&nbsp;</span>" .$first . "" . $pre . "" . $middle . "" . $next . "" . $last . "".$gotoHtm;

    } 
    
	/** JS分页处理
	 * @param int $total  总记录数
	 * @param int $page 当前页数
	 * @param int $size 每页显示
	 * @param string $jsFunction 调用页面上的JS方法名
     * @return string
	 *  */
	static function getJsPage($total, $page, $size, $jsFunction = "topage"){
		//-----------分页处理--------------------------				
		$z_totalPage = ceil ( $total / $size );
		$pageList = "<span>总数".$total."条  每页".$size."条  页次$page/$z_totalPage </span>";
		$pageList .= ($page-1)<1 ? " <a href='javascript:void(0)'>第一页</a> <a href='javascript:void(0)'>上一页</a>" : " <a href='javascript:".$jsFunction."(1)'>第一页</a> <a href='javascript:".$jsFunction."(".($page-1).")'>上一页</a>";
        
        $middlePageNum=4;
        $front = $page - $middlePageNum;
        $after = $page + $middlePageNum;
        if($front<1) $front=1;
        if($after>$z_totalPage) $after=$z_totalPage;
        
        for($i = $front; $i <= $after; $i++) {
        	$cla='';
        	$plink ="javascript:topage(".$i.");";
        	if($i==$page){$cla =" class='cur'"; $plink='javascript:void(0)';}
        	$pageList .= " <a href='$plink'$cla>$i</a>";
        }
        $pageList .= ($page+1)>$z_totalPage ? " <a href='javascript:void(0)'>下一页</a> <a href='javascript:void(0)'>未页</a>" : " <a href='javascript:".$jsFunction."(".($page+1).")'>下一页</a> <a href='javascript:".$jsFunction."(".$z_totalPage.")'>未页</a>";
		return $pageList;
	}
}

?>
